import { type RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/index.vue'
import MapRouterList from './routes/map'
import RecordRouterList from './routes/record'
import OperationRouterList from './routes/operation/index'
import ExpenseRouterList from './routes/expense/index'
import PersonnelRouterList from './routes/personnel/index'
import CustomerRouterList from './routes/account'
import EquipmentsRouterList from './routes/equipment/index'
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    component: () => import('@/components/core/login.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    meta: {
      title: '首页',
      affix: true,
      icon: 'home'
    },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/components/core/home.vue'),
        meta: {
          title: '首页',
          keepAlive: true,
          affix: true,
          icon: 'home'
        }
      }
    ]
  },
  EquipmentsRouterList,
  MapRouterList,
  PersonnelRouterList,
  ExpenseRouterList,
  OperationRouterList,
  RecordRouterList,
  {
    path: '/updatelog',
    component: Layout,
    meta: {
      title: '更新日志',
      icon: 'updatelog'
    },
    children: [
      {
        path: 'updatelog',
        name: 'Updatelog',
        component: () => import('@/views/updatelog/index.vue'),
        meta: {
          title: '更新日志',
          keepAlive: true,
          icon: 'updatelog'
        }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    meta: {
      hidden: true
    },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/403',
    component: () => import('@/views/error-page/403.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    meta: {
      hidden: true
    },
    alias: '/:pathMatch(.*)*'
  }
]
export const asyncRoutes: RouteRecordRaw[] = [CustomerRouterList]
/** 重置路由 */
export function resetRouter() {
  //所有动态路由必须带有Name属性 否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.role) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    //强制刷新浏览器也行 只是交互体验不好
    window.location.reload()
  }
}
const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})

export default router
